package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceMelService;
import com.eastjet.maintenance.domain.DeviceEfd;
import com.eastjet.maintenance.domain.DeviceMel;
import com.eastjet.maintenance.domain.DeviceRkb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mel")
public class MelController {

    @Autowired
    private DeviceMelService deviceMelService;

    @RequestMapping("/list")
    public Page<DeviceMel> list(
            @RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);

        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            // 返回空的 Page<DeviceFt> 对象
            return  deviceMelService.getDeviceMelList(0, 0, "");
        }

        Integer page = (Integer) map.get("page");
        Integer size = (Integer) map.get("size");
        String condition = (String) map.get("condition");

        System.out.println("condition:" + condition);
        System.out.println("page:" + page);
        System.out.println("size:" + size);
        return deviceMelService.getDeviceMelList(page, size, condition);
    }

    @RequestMapping("/add")
    public String add(@RequestBody Map<String, Object> map) {
        String TableName;
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        // 检查输入的表名称是否为空
        if (map.get("TableName") == null || map.isEmpty()){
            //返回错误信息
            return "TableName is null or empty";
        }else
        {
            TableName = (String) map.get("TableName");
            DeviceMel deviceMel = new DeviceMel();
            deviceMel.setCode((String) map.get("code"));
            // 使用 Integer.valueOf 转换整数
            deviceMel.setMelName((String) map.get("name"));
            deviceMel.setMelEquipmentId(Integer.valueOf(map.get("melEquipmentId").toString()));
            deviceMel.setMelPartId((String) map.get("melPartId"));
            deviceMel.setMelTypeId((String) map.get("melTypeId"));
            deviceMel.setMelStandard((String) map.get("melStandard"));
            deviceMel.setMelStandard((String) map.get("demand"));
            deviceMel.setNotes((String) map.get("notes"));
//            deviceMel.setDemand((String) map.get("demand"));
            deviceMel.setMelState(Integer.valueOf(map.get("state").toString()));
            return deviceMelService.addDeviceMel(deviceMel,TableName);
        }
    }

    @RequestMapping("/update")
    public String update(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceMel deviceMel = new DeviceMel();
        deviceMel.setCode((String) map.get("code"));
        // 使用 Integer.valueOf 转换整数
        deviceMel.setMelName((String) map.get("name"));
        deviceMel.setMelEquipmentId(Integer.valueOf(map.get("melEquipmentId").toString()));
        deviceMel.setMelPartId((String) map.get("melPartId"));
        deviceMel.setMelTypeId((String) map.get("melTypeId"));
        deviceMel.setMelStandard((String) map.get("melStandard"));
        deviceMel.setMelStandard((String) map.get("demand"));
        deviceMel.setNotes((String) map.get("notes"));
//        deviceMel.setDemand((String) map.get("demand"));
        deviceMel.setMelState(Integer.valueOf(map.get("state").toString()));;
        deviceMel.setMelId(Integer.valueOf(map.get("id").toString()));
        return deviceMelService.updateDeviceMel(deviceMel);

    }

    @RequestMapping("/delete")
    public String delete(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);

        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }

        // 获取 ids 列表
        List<Integer> ids = (List<Integer>) map.get("ids");

        // 创建 DeviceFt 对象并设置其状态
      DeviceMel deviceMel = new DeviceMel();
        deviceMel.setIsDelete(1);



        // 循环打印 ids 列表中的每个 ID
        if (ids != null) {
            for (Integer id : ids) {
                System.out.println("id: " + id);
                deviceMel.setMelId(id);
                deviceMelService.deleteDeviceMel(deviceMel);
            }
            return "success";
        } else {
            System.out.println("ids 列表为空或未提供");
            return "参数错误";
        }
    }

    @RequestMapping("/updateState")
    public String updateState(@RequestBody Map<String, Object> map) {
        System.out.println("map:" + map);
        // 检查 map 是否为空
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }

        // 获取 state 值
        Integer state = (Integer) map.get("state");
        // 获取 ids 列表
        List<Integer> ids = (List<Integer>) map.get("ids");


        DeviceMel deviceMel = new DeviceMel();
        deviceMel.setMelState(state);

        // 打印获取到的 state 值
        System.out.println("state: " + state);

        // 循环打印 ids 列表中的每个 ID
        if (ids != null) {
            for (Integer id : ids) {
                System.out.println("id: " + id);
                deviceMel.setMelId(id);
                deviceMelService.updateDeviceMelState(deviceMel);
            }
            return "success";
        } else {
            System.out.println("ids 列表为空或未提供");
            return "参数错误";
        }
    }
}
